#include "chapter1_2.h"
using namespace std;

/***********************************************************************
	ofstream open mode:
	ifstream open mode:
		ios:: app(end), ate(nd),binary, in(put),out(put),trunc(ate)
	
	normal operation:
		get/getline
		read
		seekg	-----	ios_base::beg, cur, end
		tellg
	extention operation:
		peek()	-----	get one char but no fp changed


************************************************************************/

void love(int a)
{
	static int b = a;
}

void love(string str)
{
	static string strs = str;
}

void testAbstractContaner()
{
	string file_name("test.txt");

	string cfirst, clast;

	love(2.3);
	love("Yes love");
	printf("0x%x", 2);
	printf("0x%x", 2,
			"%d", 3
		);
	 	
	void testMapSet();
	testMapSet();
	//cin>>file_name;
	//open one file in "read" mode
	ifstream infile(file_name.c_str(), ios::in);
	if(infile == NULL)
		cout<<"err open input rfile!"<<"\n", exit(-1);

	//allocate one ptr about string-container object
	vector<string> *lines_of_text = new vector<string>;
	string textline;
	typedef pair<string::size_type,int > stats;
	stats maxline;

	int lineNum = 0;

	// read line text
	cout<< "Read file:" <<"\n";
	while(getline(infile, textline, '\n')){
		cout<< textline << "\n";
		if(maxline.first < textline.size()){
			//update max linesize and its line num
			maxline.first = textline.size();
			maxline.second = lineNum;		
		}

		lines_of_text->push_back(textline);
		lineNum++;		
		textline.find("love");
		if( strstr(textline.c_str(), "love") != NULL)
			cout<<"find love !"<<"\n";
		
		string::size_type pos;

		if( (pos = textline.find(' ')) != string::npos)
			cout<<"find space !"<<"\n", textline[pos] += 32;//textline.erase(pos, 1);
		if( (pos = textline.find_first_of("bc")) != string::npos)	// find any match char of "bc"---b or c in src string
			cout<<"find love !"<<"\n";
	}

	infile.close();
	infile.open(file_name.c_str(),ios::in);
	int first = infile.peek();
	ofstream outfile("test_out.txt", ios::app);
	outfile<<hex << "test write stream to file !";
	outfile<<hex << 12345;
	outfile.close();

}

#include <map>
void testMapSet()
{
	typedef pair<short, short> Location; //x-y coordinate
	typedef vector<Location> LocXY;	//location info vectors, with x-y coordinate
	typedef map<string ,LocXY> TextMap;// member info, string is the index !

	TextMap tmpMap;
	LocXY xyVec;
	Location xy(0,1);

	xyVec.push_back(xy);
	tmpMap["usa"] = xyVec;

	map<string, int> tstmap;
	tstmap["china"] = 1;
	tstmap["china"] = 2;
	tstmap.insert(map<string, int>::value_type(string("chin"), 3));




}